﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using DataTransferObjects;
using DataAccessObject;
using Cryto_Provider;

namespace Business
{
    public class KeyBUS
    {
        public static bool generateNewKey(KeyInforDTO keyInforDTO)
        {
            RSACryptoServiceProvider rsa;            

            //Be careful with this line. If it doesn't exist, it's terrible???                         
            //CspParameters cspParams = new CspParameters();

            rsa = new RSACryptoServiceProvider(keyInforDTO.keyLength);
            RSAParameters rsaPara = new RSAParameters();
            rsaPara = rsa.ExportParameters(true);

            keyInforDTO.rsaExponent = new byte[rsaPara.Exponent.Length];
            Buffer.BlockCopy(rsaPara.Exponent, 0, keyInforDTO.rsaExponent, 0, rsaPara.Exponent.Length);
            keyInforDTO.rsaModulus = new byte[rsaPara.Modulus.Length];
            Buffer.BlockCopy(rsaPara.Modulus, 0, keyInforDTO.rsaModulus, 0, rsaPara.Modulus.Length);
            keyInforDTO.rsaD = new byte[rsaPara.D.Length];
            Buffer.BlockCopy(rsaPara.D, 0, keyInforDTO.rsaD, 0, rsaPara.D.Length);

            KeyDAO.exportKeyInforToDB(keyInforDTO);
            return true;
        }

        public static bool changeExistKey(KeyInforDTO keyInforDTO)
        {
            RSACryptoServiceProvider rsa;

            //Be careful with this line. If it doesn't exist, it's terrible???                         
            //CspParameters cspParams = new CspParameters();

            rsa = new RSACryptoServiceProvider(keyInforDTO.keyLength);
            RSAParameters rsaPara = new RSAParameters();
            rsaPara = rsa.ExportParameters(true);

            keyInforDTO.rsaExponent = new byte[rsaPara.Exponent.Length];
            Buffer.BlockCopy(rsaPara.Exponent, 0, keyInforDTO.rsaExponent, 0, rsaPara.Exponent.Length);
            keyInforDTO.rsaModulus = new byte[rsaPara.Modulus.Length];
            Buffer.BlockCopy(rsaPara.Modulus, 0, keyInforDTO.rsaModulus, 0, rsaPara.Modulus.Length);
            keyInforDTO.rsaD = new byte[rsaPara.D.Length];
            Buffer.BlockCopy(rsaPara.D, 0, keyInforDTO.rsaD, 0, rsaPara.D.Length);

            KeyDAO.changeKeyInfor(keyInforDTO);
            return true;
        }

        public static KeyInforDTO getKeyInforFromDB(int iUserID)
        {
            return KeyDAO.getKeyInforFromDB(iUserID);
        }

        //public static void exportKeyToFile(int iUserID)
        //{
        //    KeyDAO.exportKeyToFile(iUserID);
        //}

        //public static void importKeyFormFile(string strInput)
        //{
        //    KeyDAO.importKeyFormFile(strInput);
        //}

        public static bool IsPassphraseValid(int iUserID, string passPhrase)
        {
            bool bIsPassphraseValid = true;

            //KeyInforDTO keyInfor = KeyBUS.getKeyInforFromDB(iUserID);
            //Hash_Provider hash = new Hash_Provider();

            //if (!byte.Equals(hash.PassHash(passPhrase, keyInfor.saltValue), keyInfor.hashValue))
            //{
            //    bIsPassphraseValid = false;
            //}

            return bIsPassphraseValid;
        }
    }
}
